package com.nondo.dean.platform.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.dean.platform.domain.Province;
import com.nondo.dean.platform.domain.ZoneProvince;
import com.nondo.dean.platform.domain.ZoneProvinceExample;
import com.nondo.dean.platform.mapper.ProvinceMapper;
import com.nondo.dean.platform.mapper.ZoneProvinceMapper;
import com.nondo.dean.platform.model.AreaBean;

/**
 * 区域省份关系管理业务类
 * 
 * @author zhaoljin
 * @date 2018年4月3日
 */
@Component
public class ZoneProvinceService {
    
    @Autowired
    private ProvinceMapper provinceMapper;

    @Autowired
    private ZoneProvinceMapper zoneProvinceMapper;
    
    /**
     * 插入区域省份关系
     * @param zoneProvince
     * @return
     */
    @Transactional
    public int addZoneProvince(ZoneProvince zoneProvince) {
        return zoneProvinceMapper.insertSelective(zoneProvince);
    }

    /**
     * 修改区域省份关系
     * @param zoneProvince
     * @return
     */
    @Transactional
    public int updateZoneProvince(ZoneProvince zoneProvince) {
        return zoneProvinceMapper.updateByPrimaryKeySelective(zoneProvince);
    }
    
    /**
     * 删除区域所有省份关系
     * @param zoneId 区域ID
     * @return
     */
    @Transactional
    public int updateZoneAllProvince(int zoneId) {
        ZoneProvince zoneProvince = new ZoneProvince();
        zoneProvince.setDelFlag(String.valueOf(1));
        ZoneProvinceExample example = new ZoneProvinceExample();
        ZoneProvinceExample.Criteria criteria = example.createCriteria();
        criteria.andZoneIdEqualTo(zoneId);
        return zoneProvinceMapper.updateByExampleSelective(zoneProvince, example);
    }

    /**
     * 按照主键ID查询区域省份关系
     * @param id
     * @return
     */
    public ZoneProvince selectZoneProvince(Integer id) {
        return zoneProvinceMapper.selectByPrimaryKey(id);
    }

    /**
     * 区域省份关系分页查询列表
     * @param offset
     * @param limit
     * @param zoneProvince
     * @return
     */
    public List<ZoneProvince> getZoneProvinceList(int offset, int limit, ZoneProvince zoneProvince) {
        ZoneProvinceExample example = new ZoneProvinceExample();
        example.setOrderByClause("create_date DESC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        ZoneProvinceExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, zoneProvince);
        return zoneProvinceMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    /**
     * 区域省份关系总数统计
     * @param zoneProvince
     * @return
     */
    public Long count(ZoneProvince zoneProvince) {
        ZoneProvinceExample example = new ZoneProvinceExample();
        ZoneProvinceExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, zoneProvince);
        return zoneProvinceMapper.countByExample(example);
    }

    /**
     * 组装查询条件
     * @param criteria
     * @param zoneProvince
     */
    void setCriteria(ZoneProvinceExample.Criteria criteria, ZoneProvince zoneProvince) {
        if (zoneProvince != null) {
            if (zoneProvince.getId() != null)
                criteria.andIdEqualTo(zoneProvince.getId());
            if (zoneProvince.getZoneId() != null)
                criteria.andZoneIdEqualTo(zoneProvince.getZoneId());
            if (zoneProvince.getProvinceId() != null)
                criteria.andProvinceIdEqualTo(zoneProvince.getProvinceId());
        }
        criteria.andDelFlagEqualTo(String.valueOf(0));
    }

    /**
     * 通过区域ID获取该区域下的省份列表
     * @param zoneId 大区ID
     * @return
     */
    public List<AreaBean> getProvinceListByZoneId(Integer zoneId) {
        ZoneProvinceExample example = new ZoneProvinceExample();
        ZoneProvinceExample.Criteria criteria = example.createCriteria();
        criteria.andZoneIdEqualTo(zoneId);
        criteria.andDelFlagEqualTo(String.valueOf(0));
        List<ZoneProvince> zoneProvinceList = zoneProvinceMapper.selectByExample(example);
        List<AreaBean> provinceList = new ArrayList<AreaBean>();
        for (ZoneProvince zoneProvince : zoneProvinceList) {
            Province province = provinceMapper.selectByPrimaryKey(zoneProvince.getProvinceId());
            AreaBean provinceBean = AreaBean.convertToBean(province);
            provinceList.add(provinceBean);
        }
         return provinceList;
    }

}
